package com.sbtr.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TxtUtil {

	private TxtUtil() {
	}

	private static Logger logger = LoggerFactory.getLogger(TxtUtil.class);

	/**
	 * 生成txt文件(用于生成标定文件)
	 * 
	 * @param path     路径 如 /xxx/xxx
	 * @param fileName 文件名,带后缀 如 aaa.txt
	 * @param content  txt内容
	 * @throws IOException
	 */
	public static void creatTxt(String path, String fileName, String content) throws IOException {
		File pathFile = new File(path);
		if (!pathFile.exists()) {
			pathFile.mkdirs();
		}
		String relFilePath = path + File.separator + fileName;
		File file = new File(relFilePath);
		if (!file.exists() && file.createNewFile()) {
			logger.error("文件创建成功");
		}
		try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));) {
			out.write(content);
			out.newLine();
		} catch (Exception e) {
			logger.error("文件创建失败", e);
		}
	}

	/**
	 * txt文件追加内容(用于将训练日志写到txt文件) 文件不存在时生成文件
	 * 
	 * @param path
	 * @param fileName
	 * @param contents
	 */
	public static void appendContentToTxt(String path, String fileName, List<String> contents) {
		File pathFile = new File(path);
		if (!pathFile.exists()) {
			pathFile.mkdirs();
		}
		String relFilePath = path + File.separator + fileName;
		File f = new File(relFilePath);
		try (FileWriter fw = new FileWriter(f, true); PrintWriter pw = new PrintWriter(fw);) {
			for (String content : contents) {
				pw.println(content);
				pw.flush();
				fw.flush();
			}
		} catch (Exception e) {
			logger.error(path + File.separator + fileName + "添加内容失败", e);
		}
	}

	/**
	 * 文件有没有被占用
	 * 
	 * @param path
	 * @param fileName
	 * @return
	 */
	public static boolean fileNotUse(String path, String fileName) {
		String relFilePath = path + File.separator + fileName;
		File file = new File(relFilePath);
		if (!file.exists()) {
			return true;
		}
		if (file.renameTo(file)) {
			logger.info("文件未被操作");
			return true;
		} else {
			logger.info("文件正在被操作");
			return false;
		}
	}

	/**
	 * 读取
	 * 
	 * @param file     csv文件(路径+文件名)，csv文件不存在会自动创建      
	 * @param dataList 数据      * @return      
	 */
	public static boolean exportCsv(File file, List<String[]> dataList) {
		try (FileOutputStream out = new FileOutputStream(file);
				OutputStreamWriter osw = new OutputStreamWriter(out);
				BufferedWriter bw = new BufferedWriter(osw);) {
			if (dataList != null && !dataList.isEmpty()) {
				for (String[] data : dataList) {
					String lineData = String.join(",", data);
					bw.append(lineData).append("\r");
				}
			}
		} catch (Exception e) {
			return false;
		} 
		return true;
	}

	public static boolean exportTxt(File file, List<String[]> dataList) {
		try (FileOutputStream out = new FileOutputStream(file);
				OutputStreamWriter osw = new OutputStreamWriter(out);
				BufferedWriter bw = new BufferedWriter(osw);){
			if (dataList != null && !dataList.isEmpty()) {
				for (String[] data : dataList) {
					String lineData = String.join("\t", data);
					bw.append(lineData).append("\r");
				}
			}
		} catch (Exception e) {
			return false;
		} 
		return true;
	}
}
